package com.cms.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cms.common.StringUtils;
import com.cms.constant.CmsConstant;
import com.cms.model.TrAdminUser;
import com.cms.model.UserBankVo;
import com.cms.service.ReportService;
import com.cms.service.UserBankService;
import com.google.gson.Gson;


/***
 *  会员银行账号管理
 * @author huang
 *
 */
@Controller
public class UserBankController extends BaseController{

	@Autowired
	private ReportService reportService;
	
	@Autowired
	private UserBankService userBankService;
	 
	@RequestMapping("/userBank.do")
	public String enter(HttpServletRequest req,HttpServletResponse resp,ModelMap map)throws Exception
	{
		Map<String,Object>param = new HashMap<String, Object>();
		param.put(CmsConstant.CATALOG_NAME, "USER_BANK");
		map.put("userBankType", this.reportService.getReportResult(CmsConstant.CATALOG_CODE, param));
		return "userBank/userBankMain.html";
	}
	
	@RequestMapping("/userBank/pagingUserBank.do")
	public String pagingUserBank(HttpServletRequest req,HttpServletResponse resp,ModelMap map)throws Exception
	{
		map.put("userBankList", this.reportService.pagingReportResult("pagingUserBank", this.buildQueryMap(req)));
		return "userBank/userBankList.html";
	}
	
	
	@RequestMapping("/userBank/toUpdateUserBankInfo.do")
	public String toUpdateUserBankInfo(HttpServletRequest req,HttpServletResponse resp,ModelMap map)throws Exception
	{
		int userBankId = StringUtils.str2Int(req.getParameter("userBankId"));
		UserBankVo vo = new UserBankVo();
		vo.setId(userBankId); 
		map.put("userBank", userBankService.getUserBank(vo));
		Map<String,Object>param = new HashMap<String, Object>();
		param.put(CmsConstant.CATALOG_NAME, "USER_BANK");
		map.put("userBankType", this.reportService.getReportResult(CmsConstant.CATALOG_CODE, param));
		return "userBank/userBankInfo.html";
	}
	
	
	@RequestMapping("/userBank/updateUserBankInfo.do")
	public void updateUserBankInfo(HttpServletRequest req,HttpServletResponse resp,ModelMap map)throws Exception
	{
		TrAdminUser tu = new TrAdminUser();
		UserBankVo vo = new UserBankVo();
		Map<String,Object>result = new HashMap<String, Object>();
		try
		{
			this.export(req, "userBank", vo);
			vo.setModifyUser(tu.getId());
			this.userBankService.updateUserBank(vo);
			result.put(CmsConstant.STATUS, CmsConstant.SUCCESS_STATUS);
		}
		catch (Exception e) {
			result.put(CmsConstant.STATUS, CmsConstant.FAIL_STATUS);
			result.put(CmsConstant.MSG, e.getMessage());
		}
		this.renderText(resp, new Gson().toJson(result));
	}
	
	
	@RequestMapping("/userBank/deleteUserBankInfo.do")
	public void deleteUserBankInfo(HttpServletRequest req,HttpServletResponse resp,ModelMap map)throws Exception
	{
		UserBankVo vo = new UserBankVo();
		Map<String,Object>result = new HashMap<String, Object>();
		try
		{
			this.export(req, "userBank", vo);
			this.userBankService.deleteUserBank(vo);
			result.put(CmsConstant.STATUS, CmsConstant.SUCCESS_STATUS);
		}
		catch (Exception e) {
			result.put(CmsConstant.STATUS, CmsConstant.FAIL_STATUS);
			result.put(CmsConstant.MSG, e.getMessage());
		}
		this.renderText(resp, new Gson().toJson(result));
	}
}
